<template>
  <view class="content">
    <text class="order-title">订单</text>
    <view v-for="(order, index) in orders" :key="index" class="order-item">
      <view>
        
        <text class="order-time">下单时间: {{ order.createTime }}</text>
        
        <!-- 遍历订单中的商品 -->
        <view v-for="item in order.items" 
              :key="item._id" 
              class="product-item">
          <image :src="item.imgUrl" class="product-image"></image>
          <view class="product-info">
            <text class="product-name">{{ item.name }}</text>
            <text class="product-price">¥{{ item.price }} × {{ item.count }}</text>
            <text class="product-total">小计: ¥{{ (item.price * item.count).toFixed(2) }}</text>
          </view>
        </view>
        
        <view class="order-summary">
          
          <text class="total-price">订单总价: ¥{{ order.total }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
	import config from "@/Config.js";
export default {
  data() {
    return {
      title: '我的订单',
      orders: [],  // 修改为orders以匹配新数据结构
      token: ""
    }
  },
  onLoad() {
    this.query()
  },
  onShow() {
    this.query()
  },
  methods: {
    query() {
      this.token = uni.getStorageSync('token');
      uni.request({
        url: config.config+'/indent',
        method: 'GET',
        success: (res) => {
          console.log('请求成功:', res);
          if (res.statusCode === 200) {
            // 过滤出当前用户的订单
            this.orders = res.data.filter(order => order.token === this.token);
          } else {
            console.error('请求返回状态码异常:', res.statusCode);
          }
        },
        fail: (err) => {
          console.error('请求失败:', err);
        }
      });
    }
  }
}
</script>

<style>
.content {
  padding: 20rpx;
  background-color: #f5f5f5;
}

.order-item {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);
}

.order-title {
  font-size: 32rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
  display: block;
}

.order-time {
  font-size: 24rpx;
  color: #999;
  display: block;
  margin-bottom: 20rpx;
}

.product-item {
  display: flex;
  margin-bottom: 20rpx;
  padding-bottom: 20rpx;
  border-bottom: 1rpx dashed #eee;
}

.product-image {
  width: 150rpx;
  height: 150rpx;
  border-radius: 8rpx;
  margin-right: 20rpx;
}

.product-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 28rpx;
  margin-bottom: 10rpx;
}

.product-price {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.product-total {
  font-size: 26rpx;
  color: #e74c3c;
}

.order-summary {
  text-align: right;
  margin-top: 10rpx;
}

.customer {
  font-size: 26rpx;
  color: #666;
  display: block;
  margin-bottom: 10rpx;
}

.total-price {
  font-size: 30rpx;
  font-weight: bold;
  color: #e74c3c;
}
</style>